unit u_Safra;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai, DB, Menus, XPMenu, StdCtrls, Buttons, ExtCtrls, MMJPanel,
  FMTBcd, SqlExpr, DBClient, Provider, Grids, DBGrids, JvExDBGrids,
  JvDBGrid, Mask, DBCtrls;

type
  Tf_Safra = class(TfPai)
    p_safra: TDataSetProvider;
    ds_safra: TClientDataSet;
    s_safra: TSQLDataSet;
    s_safraID_SAFRA: TIntegerField;
    s_safraDESCSAFRA: TStringField;
    ds_safraID_SAFRA: TIntegerField;
    ds_safraDESCSAFRA: TStringField;
    dbedtFAIXA: TDBEdit;
    lbl1: TLabel;
    JvDBGrid1: TJvDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnGravarClick(Sender: TObject);
    procedure btnIncluirClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  f_Safra: Tf_Safra;

implementation

uses UDm;

{$R *.dfm}

procedure Tf_Safra.FormCreate(Sender: TObject);
begin
//  inherited;

end;

procedure Tf_Safra.FormShow(Sender: TObject);
begin
//  inherited;
  if (not ds_safra.Active) then
    ds_safra.Open;
end;

procedure Tf_Safra.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if (ds_safra.Active) then
    ds_safra.Close;
  inherited;

end;

procedure Tf_Safra.btnGravarClick(Sender: TObject);
begin
  if (ds_safra.State = dsInsert) then
  begin
    if dm.c_6_genid.Active then
      dm.c_6_genid.Close;
    dm.c_6_genid.CommandText := 'SELECT CAST(GEN_ID(gen_safra, 1) AS INTEGER) AS CODIGO FROM RDB$DATABASE';
    dm.c_6_genid.Open;
    ds_safraID_SAFRA.AsInteger := dm.c_6_genid.Fields[0].AsInteger;
    dm.c_6_genid.Close;
  end;
  inherited;

end;

procedure Tf_Safra.btnIncluirClick(Sender: TObject);
begin
  inherited;
  dbedtFAIXA.SetFocus;
end;

end.
